package com.im323.ip.impl;

import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.im323.common.util.HttpClientUtils;
import com.im323.common.util.IpModel;
import com.im323.common.util.IpUtil;
import com.im323.common.util.JsonUtil;
import com.im323.ip.IpConverter;

public class IpChinaZConvert extends IpConverter {

	@Override
	public IpModel parse() {
		long sta = System.currentTimeMillis();
		String doGet = HttpClientUtils.doGet(IpUtil.URL_CHINAZ);
		IpModel ipModel = null;
		if (doGet != null) {
			try {
				JsonNode rootNode = JsonUtil.getMapper().readTree(doGet);
				if(rootNode != null) {
					String ip = rootNode.get("ip").textValue();
					String address = rootNode.get("address").textValue();
					
					String location = address.substring(0,address.indexOf(" "));
					String isp = address.replace(location, "").trim();
				
					ipModel = new IpModel();
					ipModel.setIpAddress(ip);
					ipModel.setLocation(location);
					ipModel.setIsp(isp);
					ipModel.setDelay(System.currentTimeMillis() - sta);
					ipModel.setUrl(IpUtil.URL_CHINAZ);					
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ipModel;
	}

}
